package com.spsy.pay.service.impl;

import com.spsy.common.core.domain.R;
import com.spsy.pay.domain.Pay;
import com.spsy.pay.order.WxPayUtils;
import com.spsy.pay.service.IPayService;
import com.spsy.pay.service.PayOptsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 业务支付类
 * @author: liuzhiping
 * @date: 2025/1/9 15:02
 **/
@Slf4j
@Service
public class PayOptsServiceImpl implements PayOptsService {

    @Autowired
    private WxPayUtils wxPayUtils;

    @Autowired
    private IPayService iPayService;

    @Override
    public R pay(String orderId) {

        log.info("orderId:{}",orderId);


        //公司标准写法
        Pay pay =  Pay.builder()

                .orderId(orderId)
                .payStatus(Long.valueOf("1"))
                .productId("10001")
                .appId("appid")
                .notifyUrl("")
                .nonceStr("")
                .bodyInfo("测试名称")

                .build();
        int insertCount = this.iPayService.insertPay(pay);

        log.info("insertCount:{}",insertCount);


        String result = this.wxPayUtils.wxPay(orderId);

        log.info("orderId:{} result:{}",orderId,result);


        pay.setThirdResultJson(result);

        int updateCount = this.iPayService.updatePay(pay);

        log.info("updateCount:{} ",updateCount);



        /*log.info("orderId:"+orderId+ ",result:"+result);*/
        return R.ok(result);
    }

    @Override
    public R view(String orderId) throws Exception{
        return R.ok(this.wxPayUtils.refreshWxPay(orderId));
    }
}
